﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Jy123.Web.Manage.User.Guess
{
    public partial class GuessCheckUp : System.Web.UI.Page
    {
        protected Jy123.Model.User_Guess_Main modelGuess;
        int guessID;
        protected void Page_Load(object sender, EventArgs e)
        {
            int userID;
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            guessID = Convert.ToInt32(Request.QueryString["GuessID"]);

            if (!IsPostBack)
            {
                modelGuess = new Jy123.BLL.User_Guess_Main().GetModel(guessID);
                if (modelGuess.IsCheckup)
                {
                    btnCheck.Visible = false;
                }
                else
                {
                    btnCheck.Text = "通过审核";
                }
                ShowGuessInfo();
                ShowItemList();
            }
        }

        /// <summary>
        /// 显示竞猜信息
        /// </summary>
        private void ShowGuessInfo()
        {
            Model.User_Guess_Type type = new BLL.User_Guess_Type().GetModel(modelGuess.FType);
            hfID.Value = modelGuess.ID.ToString();
            txtType.Text = type.TypeName;
            ShowUserName(modelGuess.FUser);
            txtTitle.Text = modelGuess.Title;
            txtAccount.Text = modelGuess.Account;
            lbl_Point.Text = modelGuess.Point.ToString();
            lbl_Share.Text = modelGuess.Share.ToString();
            lbl_Multiple.Text = modelGuess.Multiple.ToString();
            lbl_SumPoint.Text = modelGuess.SumPoint.ToString();
            lbl_OverTime.Text = modelGuess.OverTime.ToString();
            lbl_OpenTime.Text = modelGuess.OpenTime.ToString();
        }

        /// <summary>
        /// 显示发布用户名
        /// </summary>
        /// <param name="FUser">用户ID</param>
        private void ShowUserName(int FUser)
        {
            Jy123.Model.User_Main model = new Jy123.Model.User_Main();

            Jy123.BLL.User_Main bll = new Jy123.BLL.User_Main();

            model = bll.GetModel(Convert.ToInt32(FUser));

            lbl_User.Text = model.UserName;
        }

        protected void btnCheck_Click(object sender, EventArgs e)
        {
            modelGuess = new Jy123.BLL.User_Guess_Main().GetModel(guessID);
            if (modelGuess.IsCheckup)
            {
                modelGuess.IsCheckup = false;
                new BLL.User_Guess_Main().Update(modelGuess);
                Response.Redirect("Guess.aspx");
            }
            else
            {
                modelGuess.IsCheckup = true;
                modelGuess.OverTime = DateTime.Now.AddDays(modelGuess.GuessDays);
                modelGuess.OpenTime = modelGuess.OverTime.AddDays(modelGuess.SettlementDays);
                new BLL.User_Guess_Main().Update(modelGuess);
                new BLL.Public.State().CreateGuessState(new BLL.User_Main().GetModel(modelGuess.FUser), null, modelGuess, 1);//添加动态
                Response.Redirect("Guess.aspx");
            }
           
           
        }

        /// <summary>
        /// 显示竞猜项
        /// </summary>
        /// <param name="id"></param>
        private void ShowItemList()
        {
            BLL.User_Guess_Item bllItem = new Jy123.BLL.User_Guess_Item();
            Rpt_ItemList.DataSource = bllItem.GetList("FGuess = " + modelGuess.ID.ToString());
            Rpt_ItemList.DataBind();
        }

        /// <summary>
        /// 显示正确项
        /// </summary>
        /// <param name="flag">是否正确</param>
        /// <returns>返回正确项HTML</returns>
        protected string IsRightHtml(object flag)
        {

            if (Convert.ToInt32(flag) == 1)
            {
                return "<a href='javascript:' class='btnsty'>正确项</a>";
            }
            else
            {
                return "";
            }
        }
        
    }
}
